<?php
if (! isset($_SESSION)) session_start();
?>
<script type="text/javascript" src="/modules/general/functions.js"></script>
<script type="text/javascript">
<!--
add_css("/css/corners.css");
add_css("/modules/templates/prc/css/003_corners.css");
add_js ("/modules/course_boxes/admin/functions.js");
//-->
</script>
<h2>Course Box Admin</h2>
<?php 
if (! isset($_SESSION["username"])) {
	echo "You are not auhtorized to change this content";
	exit;
}


function show_color_select ($i, $style_color) {
	$colors = array("blue","green","orange","purple","red", "yellow");
	echo "<select onchange=\"change_box_color ({$i})\" id=color_{$i}>";
	foreach ($colors as $color) {
		echo "<option";
		if ($color == $style_color) echo " selected ";
		echo ">{$color}";
	}
	echo "</select>";
}

function show_font_color_select ($i, $style_color) {
	$colors = array("black", "white");
	echo "<select onchange=\"change_box_font_color ({$i})\" id=font_color_{$i}>";
	foreach ($colors as $color) {
		echo "<option";
		if ($color == $style_color) echo " selected ";
		echo ">{$color}";
	}
	echo "</select>";
}



//Check existing set up

chdir("{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes");
if (! is_dir("templates")) {
	mkdir ("templates");
	
	
	for ($i=1;$i<=6;$i++){
		$header_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_header.dat";
		$brief_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_brief.dat";
		$content_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_content.dat";
		$url_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_url.dat";
		$color_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_color.dat";
		file_put_contents("{$header_file}", "Box {$i} Header");
		file_put_contents("{$brief_file}", "Box {$i} Brief");
		file_put_contents("{$content_file}", "Box {$i} Additional Content");
		file_put_contents("{$url_file}", "");
		file_put_contents("{$color_file}", "yellow");
		
	}
	echo "Templates created";
}

echo "<div>";
for ($i=1;$i<=6;$i++){
	$header_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_header.dat";
	$brief_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_brief.dat";
	$content_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_content.dat";
	$url_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_url.dat";
	$color_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_color.dat";
	$font_color_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/course_boxes/templates/{$i}_font_color.dat";
	
	if (file_exists($header_file)) $header = file_get_contents($header_file);
	else $header = "Box {$i} Header";
	
	if (file_exists($brief_file)) $brief = file_get_contents($brief_file);
	else $brief = "Box {$i} Brief";
	
	if (file_exists($content_file)) $content = file_get_contents($content_file);
	else $content = "Box {$i} Additional Content";
	
	if (file_exists($url_file)) $url = file_get_contents($url_file);
	else $url = "";
	
	if (file_exists($font_color_file)) $font_color = file_get_contents($font_color_file);
	else $font_color = "black";
	
	if (file_exists($color_file)) $color = file_get_contents($color_file);
	else $color = "yellow";
	
	echo <<<STOP
	<fieldset id=box_{$i} style="float:left;color:{$font_color}" class="{$color}_gradient">
	<legend>Course Box {$i}</legend>
	<div>Header {$i}<input id=header_{$i} value="{$header}"></div>
	<div>Brief {$i}<input id=brief_{$i} value="{$brief}"></div>
	<div>Additonal Content {$i}<input id=content_{$i} value="{$content}"></div>
	<div>Url {$i}<input id=url_{$i} value="{$url}"></div>
	<div>Gradient Color {$i}
STOP;
    show_color_select ($i, $color);
	echo <<<STOP
	
	
	</div>
	<div>Text Color{$i}
STOP;
	show_font_color_select ($i, $font_color);
	echo <<<STOP
	</div>
	</fieldset>
STOP;
if ($i==3) echo "</div><div>";
}
echo "</div>";
?>

<button onclick="save_course_box_details();">Save Course Box Details</button>
<div id=course_box_save_status></div>
